<%@ page import="com.party.core.model.system.TargetType" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>相册分类</title>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/table.css">
    <link rel="stylesheet" href="${ctx}/css/ui/photo/list.css">

    <%@include file="../include/commonFile.jsp" %>
</head>
<body>
<!--头部-->
<%@include file="../include/header.jsp" %>
<div class="index-outside">

    <%@include file="../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main layui-form">

            <form class="layui-form" action="${ctx}/photo/imgInfo/list.do" id="myForm" method="post">
                <input type="hidden" name="pageNo" id="pageNo"/>
                <input type="hidden" name="albumId" value="${tag.id}"/>
            </form>

            <div class="buttons mt20">
                <input type="checkbox" lay-skin="primary" title="本页全选" id="checkAll" lay-filter="checkAll"/>
                <c:if test="${tag.name != '精选'}">
                    <a class="layui-btn layui-btn-primary" onclick="setChoiceness('')">设为精选</a>
                </c:if>
                <a class="layui-btn layui-btn-primary" onclick="moveImg('')">移动照片</a>
                <c:if test="${tag.name != '热门'}">
                    <a class="layui-btn layui-btn-primary" onclick="deleteMore('${tag.id}')">删除</a>
                    <c:if test="${tag.name != '精选'}">
                        <a class="layui-btn layui-btn-danger" style="width: 150px"
                           href="javascript:txz.expireCheck('', function() { uploadPic('${tag.id}', '${tag.photosId}') })">上传照片</a>
                    </c:if>
                </c:if>
            </div>
            <div class="albums mt20">
                <c:forEach var="imgInfo" items="${list}">
                    <div class="img-item"  swipeitem="true" swipekey="${imgInfo.id}" swipesrc="${imgInfo.url}" swipew="${imgInfo.width}"
                         swiperatio="${imgInfo.ratio}" swipename="${imgInfo.name}"
                         swipeshoottime="<fmt:formatDate value="${imgInfo.shootTime}" pattern="yyyy/MM/dd HH:mm" />"
                         swipeupdatetime="<fmt:formatDate value="${imgInfo.updateDate}" pattern="yyyy/MM/dd HH:mm" />"
                         swipecamara="${imgInfo.cameraType}" swipesize="${imgInfo.size}"
                         data-tag="${tag.id}">
                        <div class="bg-img" style="<c:if
                                test="${imgInfo.url!= null}">background-image: url('${imgInfo.url}?imageMogr2/auto-orient/thumbnail/330x330')</c:if>">
                            <div class="box-check" swipeinfo="stopClick">
                                <input type="checkbox" lay-skin="primary" class="img-check" name="id" title=""
                                       value="${imgInfo.id}"/>
                            </div>
                        </div>
                        <div class="img-title" swipeinfo="stopClick">
                            <c:if test="${tag.name != '精选'}">
                                <span onclick="setChoiceness('${imgInfo.id}')">精选</span>
                            </c:if>
                            <span onclick="moveImg('${imgInfo.id}')">移动</span>
                            <c:if test="${tag.name != '热门'}">
                                <span onclick="deleteOne('${tag.id}', '${imgInfo.id}')">删除</span>
                            </c:if>
                        </div>
                    </div>
                </c:forEach>
            </div>

            <div id="page_content" class="page-container"></div>
        </div><!--底部-->
        <%@include file="../include/footer.jsp" %>
    </section>
</div>
<%@include file="../include/picBrowser.jsp" %>
<script type="text/javascript" src="${ctx}/script/common/list.js"></script>
<script type="text/javascript" src="${ctx}/script/common/table_option.js"></script>

<script type="text/javascript">

    var form;
    initPicBrowserDOM('.albums', [
    <c:if test="${tag.name != '精选'}">
    {
        icon: 'icon-choiceness',
        name: '精选',
        fn: function (imgKey) {
            setChoiceness(imgKey)
        }
    },
    </c:if>
    {
        icon: 'icon-move1',
        name: '移动',
        fn: function (imgKey) {
            moveImg(imgKey)
        }
    }
    <c:if test="${tag.name != '热门'}">
    ,{
        icon: 'icon-delete',
        name: '删除',
        fn: function (imgKey) {
            var $item = $('[swipekey="'+imgKey+'"]')
            deleteOne($item.data('tag'), imgKey)
        }
    }
    </c:if>
    ])
    $(function () {
        //加载分页
        loadPageNew({
            elem: 'page_content',
            totalCount: '${page.totalCount}',
            limit: '${page.limit}',
            page: '${page.page}'
        });

        layui.use(['form'], function () {
            form = layui.form;

            form.on('checkbox(checkAll)', function (data) {
                var checked = data.elem.checked;
                if (checked) {
                    $(".img-check").each(function (index, ele) {
                        var disabled = $(ele).prop("disabled");
                        if (!disabled) {
                            $(ele).prop("checked", true);
                        }
                    });
                } else {
                    $(".img-check").each(function (index, ele) {
                        var disabled = $(ele).prop("disabled");
                        if (!disabled) {
                            $(ele).prop("checked", false);
                        }
                    });
                }
                form.render('checkbox');
            })
        });
    });

    function moveImg(imgIds) {
        if (!imgIds) {
            var imgs = $(".img-check:checked");
            var length = imgs.length;
            if (length == 0) {
                util.layerMsgError("请先选择图片")
                return false;
            }
            var _idArray = new Array();
            imgs.each(function (index, ele) {
                _idArray.push($(ele).val());
            });
            imgIds = _idArray.join(",");
        }
        openDialog('移动照片', '${ctx}/photo/imgInfo/toMove.do?photosId=' + '${tag.photosId}'+'&tagId='+'${tag.id}', '565px', '280px', imgIds);
    }

    function openDialog(title, url, width, height, imgIds, cb, target) {
        var index = layer.open({
            type: 2,
            area: [width, height],
            title: title,
            maxmin: true, //开启最大化最小化按钮
            content: url,
            btn: ['确定', '关闭'],
            yes: function (index, layero) {
                var body = layer.getChildFrame('body', index);
                var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                var inputForm = body.find('#inputForm');
                var top_iframe;
                if (target) {
                    top_iframe = target;//如果指定了iframe，则在改frame中跳转
                } else {
                    top_iframe = '_parent';//获取当前active的tab的iframe
                }
                inputForm.attr("target", top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示

                if (iframeWin.contentWindow.doSubmit(imgIds, '${tag.id}')) {
                    setTimeout(function () {
                        top.layer.close(index);
                    }, 100);//延时0.1秒，对应360 7.1版本bug

                    setTimeout(function () {
                        window.location.reload();
                    }, 200);
                }
            },
            cancel: function (index) {
            }
        });
    }

    function deleteMore(tagId) {
        var imgs = $(".img-check:checked");
        var length = imgs.length;
        if (length == 0) {
            util.layerMsgError("请先选择图片")
            return false;
        }

        layer.confirm("你选择了" + length + "张照片, 确认删除吗？", {
            icon: 3,
            title: '系统提示'
        }, function (index) {
            //loading层
            var loadIndex = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            var _idArray = new Array();
            imgs.each(function (index, ele) {
                _idArray.push($(ele).val());
            });
            var ids = _idArray.join(",");
            deleteImg(tagId, ids);
        });
    }

    function deleteOne(tagId, imgIds) {
        layer.confirm("确认要删除该照片吗？", {
            icon: 3,
            title: '系统提示'
        }, function (index) {
            deleteImg(tagId, imgIds);
        });
    }

    function deleteImg(tagId, imgIds) {
        $.post('${ctx}/photo/imgInfo/delete.do', {
            tagId: tagId,
            imgIds: imgIds
        }, function (ret) {
            if (ret.success) {
                util.layerMsgSuccess("删除成功", function () {
                    location.reload();
                })
            } else {
                util.layerMsgError(ret.description)
            }
        });
    }

    function uploadPic(albumId, photosId) {
        txz.openPhotoUpload({
            tagId: albumId,
            id: photosId
        })
    }

    function setChoiceness(imgIds) {
        if (!imgIds) {
            var imgs = $(".img-check:checked");
            var length = imgs.length;
            if (length == 0) {
                util.layerMsgError("请先选择图片")
                return false;
            }
            var _idArray = new Array();
            imgs.each(function (index, ele) {
                _idArray.push($(ele).val());
            });
            imgIds = _idArray.join(",");
        }

        layer.confirm("确认设为精选吗？", {
            icon: 3,
            title: '系统提示'
        }, function (index) {
            var loadIndex = layer.load(1, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
            });
            $.post('${ctx}/photo/imgInfo/setChoiceness.do', {
                imgIds: imgIds,
                photosId: '${tag.photosId}'
            }, function (ret) {
                if (ret.success) {
                    util.layerMsgSuccess("设为精选成功", function () {
                        $(".img-check").each(function (index, ele) {
                            var disabled = $(ele).prop("disabled");
                            if (!disabled) {
                                $(ele).prop("checked", false);
                            }
                        });
                        $('#checkAll').prop("checked", false);
                        form.render('checkbox');
                    });
                } else {
                    util.layerMsgError(ret.description)
                }
                top.layer.close(loadIndex);
            });
        });
    }
</script>
<script type="text/javascript">
    txz.initHeader({
        nav: [{
            name: '活动相册管理',
            href: '${ctx}/photo/photoproject/list.do'
        },{
            name: '${photoProject.name}',
            curr: true
        }, {
            name: '相册分类',
            href: '${ctx}/photo/album/list.do?photosId=${tag.photosId}'
        }, {
            name: '${tag.name}',
            curr: true
        }],
        btns: [{
            name: '返回',
            icon: 'back',
            href: '${ctx}/photo/album/list.do?photosId=${tag.photosId}'
        }],
        info: '<span class="f12">共<b id="totalCount">${page.totalCount}</b>张照片</span>'
    });
</script>
</body>
</html>